﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Tema3.Models;
using Tema3.Services;
using System.Threading.Tasks;

namespace Tema3.Hubs
{
    public class BitcoinHub : Hub
    {

        public BitcoinHub()
        {

        }

        public void ClearContentCurrentClient()
        {
            TradeResponse response = TradesInstance.Instance.ClearStoredInfo(this.Context.ConnectionId);

            Clients.Caller.updateTotalAmount(response.Message, response.NewAmountValue, response.NewPriceValue);
        }

        public void AddToAvarage(int id, double amount, double price)
        {
            TradeResponse response = TradesInstance.Instance.AddNewTradeToClient(this.Context.ConnectionId, new BitcoinTrade(id, amount, price));

            Clients.Caller.updateTotalAmount(response.Message, response.NewAmountValue, response.NewPriceValue);
        }

        public void RemoveFromAvarage(int id)
        {
            TradeResponse response = TradesInstance.Instance.RemoveTradeFromClient(this.Context.ConnectionId, id);

            Clients.Caller.updateTotalAmount(response.Message, response.NewAmountValue, response.NewPriceValue);
        }

        public override Task OnConnected()
        {
            string responseMessage = TradesInstance.Instance.AddNewClient(this.Context.ConnectionId);

            Clients.Caller.updateMessage(responseMessage);

            return base.OnConnected();
        }

        public override Task OnDisconnected()
        {
            TradesInstance.Instance.RemoveClient(this.Context.ConnectionId);

            return base.OnDisconnected();
        }

        public override Task OnReconnected()
        {
            TradesInstance.Instance.AddNewClient(this.Context.ConnectionId);

            return base.OnReconnected();
        }

    }
}